package com.vert.job;

import com.vert.job.model.TestJob;
import com.vert.utils.ClazzScanUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.List;

/**
 * @Classname JobUtils
 * @Description TODO
 * @Date 2022/5/28 22:39
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class JobUtils {

    private static final String JOB_NAME="jobName";

    private static final String GROUP_NAME="group_name";

    private static final String TIGGER_NAME="tigger";

    private static final String TIGGER_GROUP="tigger_group";

    public static void init()  {
      try {
        SchedulerFactory schedulerFactory=new StdSchedulerFactory();
        Scheduler scheduler=schedulerFactory.getScheduler();

        List<Class<?>> cls= ClazzScanUtils.getClassPaths();
//        for (int i = 0; i < cls.size(); i++) {
//            Class< ? > cs=cls.get(i);
//            Job job=cs.getAnnotation(Job.class);
//            if(job==null){
//                continue;
//            }
//            String jobName=job.name();
//            if(!"".equals(jobName)){
//                jobName=getName(JOB_NAME,i);
//            }
//            String groupName=job.group();
//            if(!"".equals(groupName)){
//                groupName=getName(GROUP_NAME,i);
//            }
//            String tiggerName=getName(TIGGER_NAME,jobName);
//            String tiggerGroup=getName(TIGGER_GROUP,jobName);
//            int s=job.value();
//
//             JobDetail jobDetail= JobBuilder.newJob()
//            .withIdentity(jobName,groupName).build();
//
//            Trigger trigger=TriggerBuilder.newTrigger().withIdentity(tiggerName,tiggerGroup)
//              .startNow() //立即生效
//              .withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(s) //每隔2秒执行一次
//                .repeatForever()).build();
//
//            scheduler.scheduleJob(jobDetail,trigger);
//            scheduler.start();
//
//        }


        JobDetail jobDetail= JobBuilder.newJob(TestJob.class)
          .withIdentity("job1","group1").build();

        Trigger trigger=TriggerBuilder.newTrigger().withIdentity("tiagger1","tiaggerGroup1")
          .startNow() //立即生效
          .withSchedule(SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInSeconds(1000) //每隔2秒执行一次
            .repeatForever()).build();

        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();
      }catch (SchedulerException e) {
        e.printStackTrace();
      }
    }

    public static String getName(String name,int index){
        String n=name+index;
        return n;
    }

    public static String getName(String name,String name2){
        String n = name + name2;
        return n;
    }
}
